package game;

import gui.ShapedFrame;
import gui.ShapedFrameImage;

import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;

import javax.media.opengl.GL;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLContext;
import javax.media.opengl.GLDrawableFactory;
import javax.media.opengl.GLPbuffer;
import javax.media.opengl.glu.GLU;

import util.BasicGraphicsJogl;

import com.sun.opengl.util.Screenshot;

public class GameState
{
    private float size=10;
    public GameState()
    {
        GLDrawableFactory fac = GLDrawableFactory.getFactory();
        GLCapabilities glCap = new GLCapabilities();
        glCap.setAlphaBits(8);

        // Can't double buffer an offscreen buffer
        glCap.setDoubleBuffered(false);
        
        //makes a new buffer
        GLPbuffer buf = fac.createGLPbuffer(glCap, null, 500, 500, null);  
        
        //required for drawing to the buffer
        GLContext glContext = buf.createContext(null);

        BasicGraphicsJogl.createInstance(glContext);
        BasicGraphicsJogl basicGraphics = BasicGraphicsJogl.instance();
        
        
        GL gl = basicGraphics.getGL();
        gl.glShadeModel(GL.GL_SMOOTH);
        gl.glEnable(GL.GL_DEPTH_TEST);        
        gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
//        gl.glClearDepth(0.0);
        gl.glEnable(GL.GL_TEXTURE_2D);
        gl.glEnable (GL.GL_BLEND); 
        gl.glBlendFunc (GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
        gl.glViewport(0, 0, 500, 500);
        gl.glMatrixMode(GL.GL_PROJECTION);
        gl.glLoadIdentity();
        GLU glu = new GLU();
        glu.gluOrtho2D(0, 500, 500, 0);
        gl.glMatrixMode(GL.GL_MODELVIEW);
        gl.glLoadIdentity();
    }
    public void render(ShapedFrameImage shapedFrame)
    {
        BasicGraphicsJogl basicGraphics = BasicGraphicsJogl.instance();
        
        GL gl = basicGraphics.getGL();
        gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
        gl.glMatrixMode(GL.GL_MODELVIEW);
        gl.glLoadIdentity();
        gl.glBegin(GL.GL_TRIANGLES);
        gl.glColor3f(1.0f, 0.0f, 0.0f);
        gl.glVertex3f(200.0f-size, 0.0f, 0.0f);
        gl.glColor3f(1.0f, 1.0f, 1.0f);
        gl.glVertex3f(200.0f+size, 0.0f, 0.0f);
        gl.glColor3f(1.0f, 1.0f, 1.0f);
        gl.glVertex3f(0.0f, 200.0f+size, 0.0f);
        gl.glEnd();
        
        BufferedImage islandImage = Screenshot.readToBufferedImage(500,500,true);         
        shapedFrame.setImage(islandImage);
        shapedFrame.repaint();
    }
    public void update()
    {
        size++;
    }
//  // clean the old buffer&#10;     
//  context.destroy();   
//  //context is type GLContext&#10;     
//  buf.destroy();    
//  // buf is type GLPbuffer&#10;  
    
}
